package com.fencing_squad.homo.list;

import com.bumptech.glide.Glide;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.util.CustomComponent;
import com.fencing_squad.homo.util.ImageLoader;
import com.fencing_squad.homo.util.Network;
import com.fencing_squad.homo.util.function.FunctionInt;
import com.fencing_squad.homo.util.function.FunctionVoid;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class ProductProvider extends BaseItemProvider {
    private List<Product> list;
    private Context context;
    private static final int BEFORE_COUNT = 0;
    private FunctionInt exchange;

    public ProductProvider(Context context, FunctionInt exchange) {
        list = new ArrayList<>();
        this.context = context;
        this.exchange = exchange;
    }

    public class Holder {
        Image image;
        Text name;
        Text point;
        Text itemNum;
        Text number;
        Button exchange;
        Button sub;
        Button add;

        public Holder(Component component) {
            image = (Image) component.findComponentById(ResourceTable.Id_image);
            name = (Text) component.findComponentById(ResourceTable.Id_name);
            point = (Text) component.findComponentById(ResourceTable.Id_point);
            itemNum = (Text) component.findComponentById(ResourceTable.Id_itemNum);
            number = (Text) component.findComponentById(ResourceTable.Id_number);
            exchange = (Button) component.findComponentById(ResourceTable.Id_exchange);
            sub = (Button) component.findComponentById(ResourceTable.Id_sub);
            add = (Button) component.findComponentById(ResourceTable.Id_add);
        }
    }

    public void append(Product product) {
        if(list != null) {
            list.add(product);
            notifyDataChanged();
        }
    }

    public void append(List<Product> products) {
        if(list != null) {
            System.out.println("product");
            list.addAll(products);
            notifyDataChanged();
        }
    }

    @Override
    public int getCount() {
        return list == null ? BEFORE_COUNT : list.size() + BEFORE_COUNT;
    }

    @Override
    public Object getItem(int position) {
//        if (position == 0) {
//            return postUserCard;
//        }
        if (list != null && position >= BEFORE_COUNT && position < list.size() + BEFORE_COUNT){
            return list.get(position - BEFORE_COUNT);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        Product item = list.get(position - BEFORE_COUNT);
        Holder holder;
        if (convertComponent == null || !(convertComponent.getTag() instanceof Holder)) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_product, null, false);
            holder = new Holder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = convertComponent;
            holder = (Holder) cpt.getTag();
        }
        holder.name.setText(item.getName());
        holder.itemNum.setText(item.getItemNum());
        holder.number.setText(item.getNumber());
        holder.point.setText(item.getPoint());
        Glide.with(context).load(item.getImage()).listener(ImageLoader.fail(holder.image, ResourceTable.Media_icon)).into(holder.image);
        holder.add.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                item.addNumber();
                holder.number.setText(item.getNumber());
            }
        });
        holder.sub.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                item.subNumber();
                holder.number.setText(item.getNumber());
            }
        });
        holder.exchange.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                exchange.func(position);
            }
        });
        Glide.with(context).load(Network.getBaseURL()+item.getImage()).listener(ImageLoader.fail(holder.image, ResourceTable.Media_icon)).into(holder.image);
        return cpt;
    }
}
